home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / c / cuj9205.zip / 1005066A < prev    next >
Text File  |  1992-06-02  |  3KB  |  121 lines

  1. /* Listing 6 */
  2.  
  3. /*****************************************************
  4.     TIMER.C 
  5.  
  6.     Basic Routines for programming the 9513 timer
  7.     counter on the Lab Master AD.
  8.  
  9.     Copyright Don Bradley, 1991.
  10.  
  11.     Permission is granted for used of these routines
  12.     in any manner as long as this copyright notice is
  13.     included.
  14.  
  15.     Tested using Quick C 2.5 and MSC 6.0 on a 
  16.     Toshiba T5200.
  17.  
  18.  *****************************************************/
  19.  
  20. #include <math.h>
  21. #include <conio.h>
  22.  
  23. #include "labmastr.h"
  24.  
  25. void timer_reset()
  26.     {
  27.     /* reset 9513 timer\counter */
  28.     outp(TIMER_CONTROL, 0xFF);
  29.  
  30.     /* Enable 16-bit Data Access */
  31.     outp(TIMER_CONTROL, 0xEF);
  32.  
  33.     /* initalize master mode reg */
  34.     outp(TIMER_CONTROL, 0x17);
  35.  
  36.     /* BCD Division, 16 Bit Data */
  37.     outpw(TIMER_DATA, 0xA000);
  38.     }
  39.  
  40. void timer5_on()
  41. /*& Turns timer 5 on. Used to start ADC output from the
  42.      fifo thru manual or DMA control. */
  43.     {
  44.     /* turn timer 5 on */
  45.     outp(TIMER_CONTROL, 0x30);
  46.     }
  47.  
  48. void timer5_off()
  49. /*& Turns timer 5 off. Used to stop or disable ADC
  50.      output from the fifo thru manual or DMA control. */
  51.     {
  52.     /* turn timer 5 off */
  53.     outp(TIMER_CONTROL, 0xD0);
  54.     }
  55.  
  56. #define COUNTSTART  0xb
  57. #define COUNTHIGH  0xf
  58.  
  59. double timerad(double freq)
  60. /*& Sets timer 5 of the labmaster board to the desired
  61.      frequency for data collection. */
  62.     {
  63.     double time;
  64.     unsigned count = COUNTSTART;
  65.  
  66.     time = TIMER_F1 / freq;
  67.     while (1) {
  68.         if (time < 65536.0)
  69.             break;
  70.         time /= TIMER_DIVISOR;
  71.         ++count;
  72.         }
  73.     if (count <= COUNTHIGH) {
  74.         timer5_off();
  75.  
  76.         freq = TIMER_F1 / ((unsigned) (time) *
  77.             pow(10.0, (double) (count - COUNTSTART)));
  78.  
  79.         // initalize master mode register
  80.         outp(TIMER_CONTROL, 0x17);
  81.         // BCD Division, 16 Bit Data Bus counter 5 setup
  82.         outpw(TIMER_DATA, 0xA000);
  83.         outp(TIMER_CONTROL, 0x5);
  84.  
  85.         // No Gating, Count on Rising Edge,
  86.         // F(count-COUNTSTART), Count Repetitively,
  87.         // Count Down, Active High Terminal Count Pulse
  88.         // frequency range 400Hz to 4MHz
  89.         outpw(TIMER_DATA, (count << 8) | 0x21);
  90.  
  91.         outpw(TIMER_DATA, (int) time);
  92.  
  93.         timer5_on();
  94.         }
  95.     else 
  96.         freq = 0.0;
  97.     return (freq);
  98.     }
  99.  
  100. void step_timerad()
  101.     {
  102.  
  103.     timer5_off();
  104.  
  105.     // Point to Counter 5
  106.     outp(TIMER_CONTROL, 0x5);
  107.  
  108.     // Active High output, Count Down, Binary Counting,
  109.     // Count Repeatedly, Reload from Load Register
  110.     // only, No Gateing, No Special Gate, Count Falling
  111.     // Edges and Use Source 5.
  112.     outpw(TIMER_DATA, 0x1521);
  113.     outpw(TIMER_DATA, 2);
  114.  
  115.     // Load Counter 5
  116.     outp(TIMER_CONTROL, 0x50);
  117.     timer5_on()
  118.     // Step Counter 5
  119.     outp(TIMER_CONTROL, 0xF5);
  120.     }
  121.